<?php
/**
*A pagination generation class
*@class : Pagination
*@version: 1.0.0
*@author : huixinchen at baidu.com
*@useage:
* $pagi = new Pagination($url_prefix, $page_size, $mesgs_count, $pagination_size=10, array $conf);
* $navigation_str = $pagi->generate($current_page_number);
*/
class framework_base_pagination{
        public $page, $total_page, $total, $page_size, $size;
        public $prev_str = "上一页", $next_str = "下一页";
        public $class, $selected_class = "selected", $prev_class="prev", $next_class="next",
                $de_prev_class="de_prev", $de_next_class="de_next";
        public $url_prefix="", $split_char="?";
        public $para_name = "page", $target = "";
        public function __construct($para_name='page',$url_prefix, $page_size, $total, $size=9, $conf=array()){
                $this->page = 1;
                $this->page_size = $page_size;
                $this->total = $total;
                $this->total_page = intval(ceil($total/$page_size));
                $this->size = $size;
				$this->side_size = intval($size/2);
                if(!empty($conf)){
                        $configure = array("prev_str", "next_str", "class", "selected_class");
                        foreach($conf as $key => $val){
                                if(in_array($key, $configure)){
                                        $this->$val = $val;
                                }
                        }
                }
                $this->url_prefix = $url_prefix;
                if(strstr($url_prefix, '?') !== false){
                        $this->url_prefix .= "&" . $para_name . "=";
                }else{
                        $this->url_prefix .= "?" . $para_name . "=";
                }
        }
 
        public function generate($page){
        		if($this->total <= $this->page_size) return '';
                $this->page = $page;
                if(isset($this->page[$page])){
                        return $this->page_str[$page];
                }
                $page_start = 1;
                $half = intval($this->size/2);
                $page_start = max(1, $page - $half);
                $page_end = min($page_start + $this->size - 1, $this->total_page);
                $page_start = max(1, $page_end - $this->size + 1);
 
                $this->page_str[$page] = $this->build_nav_str($page_start, $page_end);
                return $this->page_str[$page];
        }
 
        private function build_nav_str($page_start, $page_end){
                $page_nums = range($page_start, $page_end);
                $target = $this->target? " target=\"{$this->target}\"" : "";
                if($this->page == 1){
                        $page_str = "<span class=\"{$this->de_prev_class}\"> {$this->prev_str} </span>";
                }else{
                        $page = $this->page - 1;
                        $page_str = "<span class=\"{$this->prev_class}\"> <a href=\"{$this->url_prefix}{$page}\"{$this->target}>{$this->prev_str}</a></span>";
                }
				if($this->total_page > $this->size && ($this->page - $this->side_size) > 1) {
					$page_str .= "<span class=\"{$this->class}\"><a href=\"{$this->url_prefix}1\"{$this->target}>1</a></span><span>…</span>";
				}
                foreach($page_nums as $p){
                        // $page_str .= ($p == $this->page) ? "<span class=\"{$this->selected_class}\">{$p}</span>"
                        // : "<span class=\"{$this->class}\"><a href=\"{$this->url_prefix}{$p}\"{$this->target}>{$p}</a></span>";
                        $class = ($p == $this->page) ? $this->selected_class : $this->class;                        $page_str .= "<span class=\"{$class}\"><a href=\"{$this->url_prefix}{$p}\"{$this->target}>{$p}</a></span>";
                }
				if($this->total_page > $this->size && ($this->total_page - $this->page) > $this->side_size) {
					$page_str .= "<span>…</span><span class=\"{$this->class}\"><a href=\"{$this->url_prefix}{$this->total_page}\"{$this->target}>{$this->total_page}</a></span>";
				}				
                if($this->page == $this->total_page){
                        $page_str .= "<span class=\"{$this->de_next_class}\"> {$this->next_str} </span>";
                }else{
                        $page = $this->page + 1;
                        $page_str .= "<span class=\"{$this->next_class}\"> <a href=\"{$this->url_prefix}{$page}\"{$this->target}>{$this->next_str}</a></span>";
                }
                return $page_str;
        }
 
        public function tidy_str(){
                ;//void
        }
 
        public function __call($func_name, $arguments){
                if(isset($this->$func_name)){
                        return $this->$func_name;
                }
        }
 
        public function __destruct(){
                unset($this->page_str);
                unset($this);
        }
}
?>
